from app import db
from datetime import datetime

class Role(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), unique=True, nullable=False)
    description = db.Column(db.String(200))
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

    # 角色和菜单的多对多关系
    menus = db.relationship('Menu', secondary='role_menu', backref=db.backref('roles', lazy=True))

    def __repr__(self):
        return f'<Role {self.name}>'

# 角色和菜单的关联表
role_menu = db.Table('role_menu',
    db.Column('role_id', db.Integer, db.ForeignKey('role.id'), primary_key=True),
    db.Column('menu_id', db.Integer, db.ForeignKey('menu.id'), primary_key=True)
)